//
//  PasswordCell.m
//  Hxcsfw
//
//  Created by qiysoft on 2017/11/7.
//  Copyright © 2017年 企友软mac. All rights reserved.
//

#import "PasswordCell.h"

@implementation PasswordCell

- (void)awakeFromNib {
    [super awakeFromNib];

    self.BView.layer.masksToBounds = YES;
    self.BView.layer.cornerRadius = 5;
    self.BView.layer.borderWidth = 1;
    self.BView.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.BView2.layer.masksToBounds = YES;
    self.BView2.layer.cornerRadius = 5;
    self.BView2.layer.borderWidth = 1;
    self.BView2.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    
    self.LogBtn.layer.masksToBounds = YES;
    self.LogBtn.layer.cornerRadius = 5;

//    _ZhangHaoTF.text = @"17735363827"; //111~125测试不同会员的账号
//    _MimaTF.text = @"363827";
//    _ZhangHaoTF.text = @"140123456789012";
//    _MimaTF.text = @"123456";
//    _ZhangHaoTF.text = @"911401006723482021";
//    _MimaTF.text = @"482021";
//    _ZhangHaoTF.text = @"15834125017";
//    _MimaTF.text = @"125017";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (IBAction)LogBtn:(id)sender {//密码登录按钮
    
    self.LogBtn.enabled = NO;//登录禁点
    if(![self checkInput] ) {
        return;
    }
    
    [self requestLogin:self.ZhangHaoTF.text password:self.MimaTF.text];
}

//登录请求
- (void)requestLogin:(NSString *)username password:(NSString *)password{

    NSDictionary *parametersDict = @{@"mode":@"native",@"service":@"login",@"content":@{@"username":username,@"password":password}};

    NSString *secret = [NSString stringWithFormat:@"%@",[WHttpTool convertToJsonData:parametersDict]];
    NSDictionary *paramsDic = @{@"params":[CommonFunc encryptWithText:secret]};


    [WHttpTool POST:[WShareUtil getServerURL:nil] parameters:@{@"params":[CommonFunc encryptWithText:secret]} success:^(id responseObject) {
        WLog(@"------%@",responseObject);
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSDictionary *JsonDic = [WHttpTool dictionaryWithJsonString:responseStr];
        NSString *messageStr = [JsonDic objectForKey:@"message"];
        NSString *successStr = [JsonDic objectForKey:@"success"];
        // 解密请求回的数据（字典）
        NSString *returnStr = [CommonFunc decryptWithText:messageStr];

        // json字符串转字典
        NSDictionary *returnDic = [WHttpTool dictionaryWithJsonString:returnStr];

        if ([successStr boolValue]) {
            [SVProgressHUD showImage:nil status:@"登录成功"];
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
            [SVProgressHUD dismissWithDelay:1.];
            [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];   //背景颜色

            [WShareUtil sharedData].dicSession = returnDic;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginSuccess" object:nil];



            [WShareUtil removeDataWithUserDefaultsForKey:@"USERNAME"];
            [WShareUtil removeDataWithUserDefaultsForKey:@"PASSWORD"];
            [WShareUtil removeDataWithUserDefaultsForKey:@"DICSESSION"];

            [WShareUtil saveDataWithUserDefaultsSetObject:username ForKey:@"USERNAME"];
            [WShareUtil saveDataWithUserDefaultsSetObject:password ForKey:@"PASSWORD"];
            [WShareUtil saveDataWithUserDefaultsSetObject:returnDic ForKey:@"DICSESSION"];


            // 登录成功直接进详情页的通知
            if ([[WShareUtil sharedData].Who isEqualToString:@"CSZX"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"CSZX" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"WDWT"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WDWT" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"ZSFWPT"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ZSFWPT" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"SJPFW"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SJPFW" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"XHAZ"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"XHAZ" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"WDXX"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WDXX" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"ZXYK"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ZXYK" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"FPMP"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"FPMP" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"WDKC"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WDKC" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"WDDD"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WDDD" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"FWYY"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"FWYY" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"SYSJPFW"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SYSJPFW" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"SYZXYK"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SYZXYK" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"SYWDXX"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SYWDXX" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"WBWDWT"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"WBWDWT" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"KCGM"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"KCGM" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"HYJF"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"HYJF" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"SWBQX"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"SWBQX" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"ASWBQX"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ASWBQX" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"HAWBQX"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"HAWBQX" object:nil];
            }else if ([[WShareUtil sharedData].Who isEqualToString:@"AWBQX"]){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"AWBQX" object:nil];
            }
            
        } else {
            [WShareUtil showAlert:@"温馨提示" message:@"用户名或密码错误！请重新输入"];
        }
        self.LogBtn.enabled = YES;

    } failure:^(NSError *error) {
        [SVProgressHUD showImage:nil status:@"登录失败"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, WSCREENH_HEIGHT *0.5 - 70)];
        [SVProgressHUD dismissWithDelay:1.];
        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:0.9 green:0.91 blue:0.93 alpha:1]];
        self.LogBtn.enabled = YES;

    } showProgress:YES];
}

- (IBAction)ForgetBtn:(id)sender {
    [WShareUtil showAlert:@"温馨提示" message:@"忘记密码未开发，尽请期待！"];
}

- (IBAction)registerBtn:(id)sender {//注册
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PASSWORDREGISTER" object:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}
/*
 _ZhangHaoTF.text = @"140123456789012";
 _MimaTF.text = @"123456";
 */
//判断登录条件
#pragma mark - check input
-(BOOL)checkInput{
    BOOL resultBool = NO;
    NSString *userNameString = self.ZhangHaoTF.text;
    if ([WShareUtil strNilOrEmpty:userNameString]) {
        [WShareUtil showAlert:@"温馨提示" message:@"用户名不能为空！"];
        self.LogBtn.enabled = YES;
//        self.btnOfBack.enabled = YES;
        return resultBool;
    }
    
    userNameString = [userNameString stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:userNameString]) {
        [WShareUtil showAlert:@"温馨提示" message:@"用户名不能全部为空格！"];
        self.LogBtn.enabled = YES;
//        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    
    if ([WShareUtil strNilOrEmpty:self.MimaTF.text]) {
        [WShareUtil showAlert:@"温馨提示" message:@"登录密码不能为空！"];
        self.LogBtn.enabled = YES;
//        self.btnOfBack.enabled = YES;
        return resultBool;
    }
    
    NSString *strPassword = self.MimaTF.text;
    strPassword = [strPassword stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([WShareUtil strNilOrEmpty:strPassword]) {
        [WShareUtil showAlert:@"温馨提示" message:@"登录密码不能全部为空格！"];
        self.LogBtn.enabled = YES;
//        self.btnOfBack.enabled = YES;
        
        return resultBool;
    }
    return YES;
}

@end
